﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwoDimArrayTaskOne
{
    class Program
    {
        static void Main(string[] args)
        {
            //int column = 5;
            //int row = 4;
            //int[,] arrayTaskOne = CreateRandomArray(row, column);   
            //PrintArrayToConsole(arrayTaskOne);
            Console.Write("Ввести количество строк и столбцов квадратного массива: ");
            int rows = Int32.Parse(Console.ReadLine());
            int columns = rows;
            int[,] squareArray = CreateRandomSquareArray(rows, columns);
            PrintArrayToConsole(squareArray);
            Console.WriteLine("");
            Console.WriteLine("");
            SummOfDiagonalElements(squareArray);
            //int[,] arrayWithSumOfDiagElem = SummOfDiagonalElements(squareArray);
            //PrintArrayToConsole(arrayWithSumOfDiagElem);
            //int [,] arrayWithReplacedElem = SumAndReplacement(arrayTaskOne);
            //PrintArrayToConsole(arrayWithReplacedElem);

            Console.Read();
        }
        static int[,] CreateRandomArray(int row, int column)
        {
            int[,] newArray = new int[row, column];
            Random rnd = new Random();

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    newArray[i, j] = rnd.Next(1, 20);
                }
            }
            return newArray;
        }

        static int[,] CreateRandomSquareArray(int rows, int columns)
        {
            int[,] squareArray = new int[rows, columns];
            Random rnd = new Random();

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    squareArray[i, j] = rnd.Next(-100, 101);
                }
            }
            return squareArray;
        }

        static void PrintArrayToConsole(int[,] arrayForPrint)
        {
            for (int i = 0; i < arrayForPrint.GetLength(0); i++)
            {
                for (int j = 0; j < arrayForPrint.GetLength(1); j++)
                {
                    Console.Write("{0}\t", arrayForPrint[i, j]);
                }
                Console.WriteLine();
            }
        }
        static int[,] SumAndReplacement(int[,] newArray)
        {
            for (int i = 0; i < newArray.GetLength(0); i++)
            {
                int summNumber = 0;
                for (int j = 0; j < newArray.GetLength(1); j++)
                {
                    int a = newArray.GetLength(1) - 1;
                    if (j == a)
                    {
                        newArray.SetValue(summNumber, i, j);
                        //newArray[i, j] = summNumber;
                    }
                    else
                    {
                        summNumber = summNumber + newArray[i, j];
                    }
                }
            }
            return newArray;
        }

        static void SummOfDiagonalElements(int[,] squareNewArray)
        {
            int summNumber = 0;
            for (int i = 0; i < squareNewArray.GetLength(0); i++)
            {
                for (int j = 0; j < squareNewArray.GetLength(1); j++)
                {
                    if (j == i)
                    {
                        summNumber = summNumber + squareNewArray[i, j];
                    }
                }
            }
            Console.WriteLine("Сумма элементов главной диагонали массива: {0}", summNumber);

        }
    }
}

